//
//  CalculatorViewController.m
//  Finance
//
//  Created by cheng xiang on 13-4-2.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "CalculatorViewController.h"
#import "AppDelegate.h"
@interface CalculatorViewController ()

@end

@implementation CalculatorViewController
@synthesize txtBuynumber;
@synthesize txtMoneyRate;
@synthesize txtTime;

@synthesize lbBankMoney;
@synthesize lbTotalBankMoney;
@synthesize lbRealMoney;
@synthesize lbTotalRealMoney;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.txtTime.delegate = self;
    self.txtMoneyRate.delegate = self;
    self.txtBuynumber.delegate = self;
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [self.navigationItem setLeftBarButtonItem:leftItem];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"计算" style:UIBarButtonItemStyleBordered target:self action:@selector(calculateValue)];
    [rightItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [rightItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [self.navigationItem setRightBarButtonItem:rightItem];
    
    //[self.btn365 setImage:[UIImage imageNamed:@"second.png"] forState:UIControlStateNormal];
    //[self.btn360 setImage:[UIImage imageNamed:@"first.png"] forState:UIControlStateNormal];
    selectionIndex = 0;
    
    UIColor* bk = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"beij.png"]];
    [self.view setBackgroundColor:bk];
    
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)goReturn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *aController = (UITabBarController *)delegate.window.rootViewController;
    [delegate hideTabBar:aController];
}

- (void)viewDidDisappear:(BOOL)animated
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UITabBarController *aController = (UITabBarController *)delegate.window.rootViewController;
    [delegate showTabBar:aController];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma - 计算

-(void)calculateValue
{
    float fRate = [self.txtMoneyRate.text floatValue]*1.0/100.0;
    float fMoneyNumber = [self.txtBuynumber.text floatValue];
    float fDay = [self.txtTime.text floatValue];
    
    int baseDay = 365;
    if (selectionIndex == 0)
    {
        baseDay = 365;
    }
    else
    {
        baseDay = 360;
    }
    
    float fRealMoney = fMoneyNumber*fRate*fDay/baseDay*1.0 + fMoneyNumber;
    
    NSString* strRealMoney = [NSString stringWithFormat:@"%.2f",fRealMoney];
    
    [self.lbRealMoney setText:strRealMoney];
}

-(IBAction)calculate:(id)sender
{


}

#pragma -
-(IBAction)select365:(id)sender
{

}

-(IBAction)select360:(id)sender
{
    selectionIndex = 1;
}

#pragma - response keyboard event
-(void)textFieldDidBeginEditing:(UITextField *)textField
{

}

-(void)textFieldDidEndEditing:(UITextField *)textField
{

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    if([self.txtBuynumber isFirstResponder])
    {
        [self.txtMoneyRate becomeFirstResponder];
    }
    else if([self.txtMoneyRate isFirstResponder])
    {
        [self.txtTime becomeFirstResponder];
    }
    else if([self.txtTime isFirstResponder])
    {
        [self.txtTime resignFirstResponder];
    }
    
    return  YES;
}

@end
